<?php

/**
 * @Project NUKEVIET 3.0
 * @Author VINADES.,JSC (contact@vinades.vn)
 * @Copyright (C) 2010 VINADES.,JSC. All rights reserved
 * @Createdate 1/9/2010, 3:21
 */

if (!defined('NV_MAINFILE'))
    die('Stop!!!');

/**
 * nv_delete_cache()
 *
 * @param mixed $pattern
 * @return
 */
function nv_delete_cache($pattern)
{
    global $lang_global, $sys_info, $global_config;
    if ($dh = opendir(NV_ROOTDIR . "/" . NV_CACHEDIR))
    {
        while (($file = readdir($dh)) !== false)
        {
            if (preg_match($pattern, $file))
            {
                unlink(NV_ROOTDIR . "/" . NV_CACHEDIR . "/" . $file);
            }
        }
        closedir($dh);
    }
}

/**
 * nv_delete_all_cache()
 *
 * @return
 */
function nv_delete_all_cache()
{
    $pattern = "/(.*)\.cache/";
    nv_delete_cache($pattern);
}

/**
 * nv_del_cache_module()
 *
 * @param mixed $module_name
 * @param mixed $lang
 * 
 * @return void
 */
function nv_del_moduleCache($module_name, $lang = NV_LANG_DATA)
{
    $pattern = "/^" . $lang . "\_" . $module_name . "\_(.*)\.cache$/i";
    nv_delete_cache($pattern);
}

/**
 * nv_get_cache()
 *
 * @param mixed $filename
 * @return
 */
function nv_get_cache($filename)
{
    if (empty($filename) or !preg_match("/(.*)\.cache/", $filename))
        return false;
    $filename = basename($filename);
    if (!file_exists(NV_ROOTDIR . "/" . NV_CACHEDIR . "/" . $filename))
        return false;

    return nv_gz_get_contents(NV_ROOTDIR . "/" . NV_CACHEDIR . "/" . $filename);
}

/**
 * nv_set_cache()
 *
 * @param mixed $filename
 * @param mixed $content
 * @return
 */
function nv_set_cache($filename, $content)
{
    if (empty($filename) or !preg_match("/(.*)\.cache/", $filename))
        return false;
    $filename = basename($filename);

    return nv_gz_put_contents(NV_ROOTDIR . "/" . NV_CACHEDIR . "/" . $filename, $content);
}

/**
 * nv_db_cache()
 *
 * @param mixed $sql
 * @param mixed $key
 * @param mixed $module_name
 * @param mixed $lang
 * @return
 */
function nv_db_cache($sql, $key = '', $modname = '', $lang = NV_LANG_DATA)
{
    global $db, $module_name;

    $list = array();

    if (empty($sql))
        return $list;

    if (empty($modname))
        $modname = $module_name;

    $cache_file = $lang . "_" . $modname . "_" . md5($sql) . "_" . NV_CACHE_PREFIX . ".cache";
    if (($cache = nv_get_cache($cache_file)) != false)
    {
        $list = unserialize($cache);
    }
    else
    {
        if (($result = $db->sql_query($sql)) !== false)
        {
            $a = 0;
            while ($row = $db->sql_fetch_assoc($result))
            {
                $key2 = (!empty($key) and isset($row[$key])) ? $row[$key] : $a;
                $list[$key2] = $row;
                ++$a;
            }

            $db->sql_freeresult($result);

            $cache = serialize($list);
            nv_set_cache($cache_file, $cache);
        }
    }

    return $list;
}
/**
 * nv_db_sub_shop_cache()
 *
 * @param mixed $sql
 * @param mixed $key
 * @param mixed $module_name
 * @param mixed $lang
 * @return
 */
function nv_db_sub_shop_cache($sql, $key = '', $modname = 'shops', $lang = NV_LANG_DATA)
{
    global $db, $module_name;

    $list = array();

    if (empty($sql))
        return $list;
        
    if (empty($modname))
        $modname = $module_name;

    $cache_file = $lang . "_" . $modname . "_sub_" . md5($sql) . "_" . NV_CACHE_PREFIX . ".cache";
    if (($cache = nv_get_cache($cache_file)) != false)
    {
        $list = unserialize($cache);
    }
    else
    {
        if (($result = $db->sql_query($sql)) !== false)
        {
            while ( list( $catid_i, $parentid_i, $lev_i, $title_i, $alias_i, $viewcat_i, $numsubcat_i, $subcatid_i, $numlinks_i, $del_cache_time_i, $description_i, $inhome_i, $keywords_i, $who_view_i, $groups_view_i ) = $db->sql_fetchrow( $result ) )
            {
                $link_i = NV_BASE_SITEURL . "index.php?" . NV_LANG_VARIABLE . "=" . NV_LANG_DATA . "&amp;" . NV_NAME_VARIABLE . "=" . $modname . "&amp;" . NV_OP_VARIABLE . "=" . $alias_i . "";
                $list[$catid_i] = array(
                    "catid" => $catid_i, "parentid" => $parentid_i, "title" => $title_i, "alias" => $alias_i, "link" => $link_i, "viewcat" => $viewcat_i, "numsubcat" => $numsubcat_i, "subcatid" => $subcatid_i, "numlinks" => $numlinks_i, "description" => $description_i, "inhome" => $inhome_i, "keywords" => $keywords_i, "who_view" => $who_view_i, "groups_view" => $groups_view_i, 'lev' => $lev_i
                );
                if ( $alias_cat_url == $alias_i )
                {
                    $catid = $catid_i;
                    $parentid = $parentid_i;
                }
            }
            $db->sql_freeresult($result);
            $cache = serialize($list);
            nv_set_cache($cache_file, $cache);
        }
    }
    return $list;
}
/**
 * nv_db_sub_shop_cache()
 *
 * @param mixed $sql
 * @param mixed $key
 * @param mixed $module_name
 * @param mixed $lang
 * @return
 */
function nv_db_sub_news_cache($sql, $key = '', $modname = 'news', $lang = NV_LANG_DATA)
{
    global $db, $module_name;

    $list = array();

    if (empty($sql))
        return $list;
        
    if (empty($modname))
        $modname = $module_name;

    $cache_file = $lang . "_" . $modname . "_sub_" . md5($sql) . "_" . NV_CACHE_PREFIX . ".cache";
    if (($cache = nv_get_cache($cache_file)) != false)
    {
        $list = unserialize($cache);
    }
    else
    {
        if (($result = $db->sql_query($sql)) !== false)
        {
            while ( $row = $db->sql_fetchrow( $result ) )
            {
                $link_i = NV_BASE_SITEURL . "index.php?" . NV_LANG_VARIABLE . "=" . NV_LANG_DATA . "&amp;" . NV_NAME_VARIABLE . "=" . $modname . "&amp;" . NV_OP_VARIABLE . "=" . $row['alias'] . "";
                $list[$row['catid']] = array(
                    "catid" => $row['catid'],
                    "parentid" => $row['parentid'],
                    "title" => $row['title'],
                    "alias" => $row['alias'],
                    "link" => $link_i,
                    "viewcat" => $row['viewcat'],
                    "subcatid" => $row['subcatid'],
                    "numlinks" => $row['numlinks'],
                    "description" => $row['description'],
                    "inhome" => $row['inhome'],
                    "keywords" => $row['keywords'],
                    "who_view" => $row['who_view'],
                    "groups_view" => $row['groups_view'],
                );
            }
            $db->sql_freeresult($result);
            $cache = serialize($list);
            nv_set_cache($cache_file, $cache);
        }
    }
    return $list;
}
?>